package com.mjk.common.ai.service;


import com.mjk.common.ai.base.ThreadContext;
import com.mjk.common.ai.dto.ConvPresetAddReq;
import com.mjk.common.ai.dto.ConvPresetEditReq;
import com.mjk.common.ai.entity.ConversationPreset;
import com.mjk.common.ai.enums.ErrorEnum;
import com.mjk.common.ai.exception.BaseException;
import com.mjk.common.core.jdbc.bean.BeePage;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.common.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ConversationPresetService {
    @Autowired
    private BeeDBService beeDBService;

    public BeePage<ConversationPreset> search(String keyword, int currentPage, int pageSize) {
        BeeQueryWrapper wrapper = new BeeQueryWrapper(ConversationPreset.class);
        wrapper.eq(ConversationPreset::getIsDeleted, false)
                .like(ConversationPreset::getTitle, keyword)
                .orderBy(" updateTime ")
                .setPage(currentPage, pageSize);
        return beeDBService.findManyByWrapperWithPage(wrapper);

    }

    public ConversationPreset addOne(ConvPresetAddReq presetAddReq) {
        if (StringUtils.isAnyBlank(presetAddReq.getTitle(), presetAddReq.getRemark())) {
            throw new BaseException(ErrorEnum.A_PARAMS_ERROR);
        }
        ConversationPreset newOne = new ConversationPreset();
        newOne.setUuid(RandomUtil.getShortUuid());
        newOne.setTitle(presetAddReq.getTitle());
        newOne.setRemark(presetAddReq.getRemark());
        newOne.setAiSystemMessage(presetAddReq.getAiSystemMessage());
        beeDBService.save(newOne);
        return newOne;
    }

    public boolean edit(ConvPresetEditReq editReq) {
        if (StringUtils.isAnyBlank(editReq.getUuid(), editReq.getTitle(), editReq.getRemark())) {
            throw new BaseException(ErrorEnum.A_PARAMS_ERROR);
        }
        if (Boolean.FALSE.equals(ThreadContext.getCurrentUser().getIsAdmin())) {
            throw new BaseException(ErrorEnum.A_USER_NOT_AUTH);
        }
        ConversationPreset preset = new ConversationPreset();
        preset.setTitle(editReq.getTitle());
        preset.setRemark(editReq.getRemark());
        preset.setAiSystemMessage(editReq.getAiSystemMessage());
        BeeQueryWrapper wrapper = new BeeQueryWrapper(preset);
        wrapper.eq(ConversationPreset::getUuid, editReq.getUuid());
        return beeDBService.update(wrapper) > 0;

    }

    public boolean softDel(String uuid) {
        if (Boolean.FALSE.equals(ThreadContext.getCurrentUser().getIsAdmin())) {
            throw new BaseException(ErrorEnum.A_USER_NOT_AUTH);
        }
        ConversationPreset preset = new ConversationPreset();
        BeeQueryWrapper wrapper = new BeeQueryWrapper(ConversationPreset.class);
        wrapper.eq(ConversationPreset::getUuid, uuid);
        preset.setIsDeleted(true);
        return beeDBService.update(wrapper) > 0;
    }
}
